package com.jadcon.addressbook.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class DbUtils {
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/addressbook", "root", "");
    }


    public static SimpleJdbcTemplate getSimpleJdbcTemplate() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/addressbook");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        return new SimpleJdbcTemplate(dataSource);
    }
    

    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ignore) {}
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ignore) {}
        }
        if (rs != null) {
            try {
                conn.close();
            } catch (SQLException ignore) {}
        }
    }
}

